package com.augurit.common.common.http.kt.request

import com.augurit.common.common.http.kt.api.IApiRequest
import com.augurit.common.common.http.kt.api.RequestConvertible
import okhttp3.MediaType
import okhttp3.RequestBody


/**
 *  author : yanghaozhang
 *  date : 2022/5/5 10:12
 *  description :
 */

typealias Parameters = List<Pair<String, String?>>

sealed class ExtraParam {
    class BodyParam(var param: RequestBody) : ExtraParam()
    class JsonParam(var param: String) : ExtraParam()
    class ObjParam(var param: Any) : ExtraParam()
    class TextParam(var param: String) : ExtraParam()
    class PairParam(var param: LinkedHashMap<String, String?>) : ExtraParam()
}

interface Request<T> : RequestConvertible<T> {

    val platform: IPlatFormStrategy

    val apiRequest: IApiRequest<T>

}

class CustomRequest<T>(override val platform: IPlatFormStrategy, private val block: suspend IPlatFormStrategy.() -> T) : Request<T> {

    override val request: Request<T> = this

    override val apiRequest: IApiRequest<T> = this::createApiRequest

    suspend fun createApiRequest(): T {
        return block.invoke(platform)
    }
}

class DefaultRequest(
    override val platform: IPlatFormStrategy,
    val method: Method,
    val url: String,
    val parameters: ExtraParam?,
    val headers: Map<String, String> = emptyMap(),
) : Request<String> {

    override val apiRequest: IApiRequest<String> = this::createApiRequest

    suspend fun createApiRequest(): String {
        val service = platform.getService(ApiService::class.java)
        return when (method) {
            Method.POST -> {
                when (parameters) {
                    is ExtraParam.BodyParam -> {
                        service.postBody(url, parameters.param, headers)
                    }
                    is ExtraParam.JsonParam -> {
                        //上传的Json
                        val body: RequestBody = RequestBody.create(
                            MediaType.parse("application/json; charset=utf-8"),
                            parameters.param
                        )
                        service.postJson(url, body, headers)
                    }
                    is ExtraParam.ObjParam -> {
                        service.postBody(url, parameters.param, headers)
                    }
                    is ExtraParam.TextParam -> {
                        //上传的文本内容
                        val body: RequestBody = RequestBody.create(MediaType.parse("text/plain"), parameters.param)
                        service.postBody(url, body, headers)
                    }
                    is ExtraParam.PairParam -> {
                        service.post(url, parameters.param.filter { it.value != null }, headers)
                    }
                    null -> {
                        service.post(url, emptyMap(), headers)
                    }
                }
            }
            Method.GET -> {
                val params = if (parameters is ExtraParam.PairParam) {
                    parameters.param.filter { it.value != null }
                } else emptyMap()
                service.get(url, params, headers)
            }
        }
    }

    override val request: Request<String> = this
}